<?php

require_once 'cf/xml.inc';
require_once 'cf/page.inc';

class SiteMap
{
	function __construct($baseUrl)
	{
		$this->baseUrl = $baseUrl;
		$this->xml = new XMLStreamWriter('sitemap.xml','1.0','UTF-8');
		
		$this->xml->startElement('urlset'); 
		$this->xml->writeAttribute('xmlns', "http://www.sitemaps.org/schemas/sitemap/0.9");
	}
	
	function __destruct()
	{
		$this->xml->endElement();    //urlset
		$this->xml->flush();
	}
	
	
	public function addUrl($url, $lastmod=null, $changefreq=null, $priority=null)
	{
		$this->xml->startElement('url');
		
		$this->xml->writeElement('loc',$this->baseUrl.$url);
		if ($lastmod)
		{
			$this->xml->writeElement('lastmod',$lastmod);
		}
		if ($changefreq)
		{
			$this->xml->writeElement('changefreq',$changefreq);
		}
		if ($priority)
		{
			$this->xml->writeElement('priority',$priority);
		}
		
		$this->xml->endElement();
	}
	
	private $baseUrl = '';
	private $xml = null;
};




function pages_to_sitemap($map, $lastmod=null, $changefreq=null, $priority=null)
{
	$pages = new DescendantPages(null);
	foreach ($pages as $page)
	{
		$map->addUrl($page->link(),$lastmod,$changefreq,$priority);
	}
}





require_once 'cf/shop.inc';

class ShopExportToSiteMapXML extends ShopExport
{
	function __construct($sitemap, $groupUrl, $productUrl, $manufacturerUrl)
	{
		$this->sitemap = $sitemap;
		$this->groupUrl = $groupUrl;
		$this->productUrl = $productUrl;
		$this->manufacturerUrl = $manufacturerUrl;
	}
	
	protected function exportGroup($group)
	{
		$this->sitemap->addUrl($this->groupUrl.$group->id());
	}
	
	protected function exportProduct($product, $groupId, $manufacturer, $option=null)
	{
		if (!$option)
		{
			$this->sitemap->addUrl($this->productUrl.$product->id());
		}
		
		static $manufacturers = array();
		
		if (!array_key_exists($manufacturer->id(), $manufacturers))
		{
			$this->sitemap->addUrl($this->manufacturerUrl.$manufacturer->id());
			$manufacturers[$manufacturer->id()] = true;
		}
	}
	
	
	private $sitemap = null;
	private $groupUrl = null;
	private $productUrl = null;
	private $manufacturerUrl = null;
};

?>